class MediaDecoder : public Runnable {
	public:
		MediaDecoder(AVCodecContext *ctx, AVStream *stream, int streamIndex, PlayerState *playerState);
		virtual ~MediaDecoder();

		virtual void start();

		virtual void stop();

		virtual void flush();

		int pushPacket(AVPacket pkt);

		int pushNullPacket();

		int getPacketSize();

		int getStreamIndex();

		AVStream *getStream();

		AVCodecContext *getCodecContext();

		int getMemorySize();

		int hasEnoughSize();

		virtual void run();

	protected:
		Mutex mMutex;
		Condition mCondition;
		PlayerState *playerState;
		PacketQueue *packetQueue;
		AVCodecContext *pCodecCtx;
		AVStream *pStream;
		int streamIndex;
};

MediaDecoder::MediaDecoder(AVCodecContext *ctx, AVStream *stream, int streamIndex, PlayerState *playerState){
	packetQueue = new PacketQueue();
	this->pCodecCtx = ctx;
	this->pStream = stream;
	this->streamIndex = streamIndex;
	this->playerState = playerState;
}

MediaDecoder::~MediaDecoder(){
	ALOGI("MediaDecoder destructor");
	stop();
	if(packetQueue){
		packetQueue->flush();
		delete packetQueue;
		packetQueue = NULL;
	}
	if(pCodecCtx){
		avcodec_close(pCodecCtx);
		avcodec_free_context(pCodecCtx);
		pCodecCtx = NULL;
	}
	playerState = NULL;
}

void MediaDecoder::start() {
	if(packetQueue){
		packetQueue->start();
	}
}

void MediaDecoder::stop() {
	if(packetQueue){
		packetQueue->abort();
	}
}

void MediaDecoder::flush() {
	if(packetQueue){
		packetQueue->flush();
	}
}

int MediaDecoder::pushPacket(AVPacket *pkt) {
	if(packetQueue){
		packetQueue->pushPacket(pkt);
	}
	return -1;
}

int MediaDecoder::pushNullPacket() {
	if(packetQueue) {
		packetQueue->pushiNullPacket(streamIndex);
	}
	return -1;
}

int MediaDecoder::getPacketSize() {
	return packetQueue != NULL ? packetQueue->getPacketSize : 0;
}

int MediaDecoder::getStreamIndex() {
	return streamIndex;
}

AVStream *MediaDecoder::getStream() {
	return pStream;
}

AVCodecContext *MediaDecoder::getAVCodecContext() {
	return pCodecCtx;
}

int MediaDecoder::getMemorySize() {
	return packetQueue != NULL ? packetQueue.getSize() : 0;
}

int MediaDecoder::hasEnoughPackets() {
	Mutex:Autolock lock(mMutex);
	return (packetQueue == NULL) || (packetQueue->isAbort()) || (pStream->disposition & AV_DISPOSITION_ATTACHED_PIC) || (packetQueue->getPacketSize() > MIN_FRAMES) && (!packetQueue->getDuration() ||  av_q2d(pStream->time_base) * packetQueue->getDuration() > 1.0);
}

void MediaDecoder::run() {

}
